package com.suixun.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suixun.dao.LogOperationMapper;
import com.suixun.dao.devicemanage.DeviceServiceDAO;
import com.suixun.dao.devicemanage.PackageServiceDAO;
import com.suixun.entity.LogOperation;
import com.suixun.entity.PackageInfo;
import com.suixun.entity.form.PackageForm;
import com.suixun.entity.page.PageModel;
import com.suixun.service.DeviceService;
import com.suixun.service.PackageService;
import com.suixun.util.DateUtil;
import com.suixun.util.StringUtil;

@Service
public class PackageServiceImpl implements PackageService {

	@Resource
	private PackageServiceDAO packagedao;
	@Resource
	private LogOperationMapper logdao;

	// 查询某个套餐明细
	@Override
	public PackageInfo queryPackageByName(PackageForm paramForm) {
		if (StringUtil.isBlank(paramForm.getPackagename()))
			return null;
		PackageInfo packageInfo = packagedao.queryPackageByName(paramForm);
		return packageInfo;
	}

	// 查询某个套餐明细
	@Override
	public PackageInfo queryPackageByID(PackageForm paramForm) {
		if (StringUtil.isBlank(paramForm.getId()))
			return null;
		PackageInfo packageInfo = packagedao.queryPackageByID(paramForm);
		return packageInfo;
	}


	// 查询套餐列表
	@Override
	public List<PackageInfo> queryPackagelist(PackageForm paramForm) {
		if (!StringUtil.isBlank(paramForm.getPackagename()))
			paramForm.setPackagename(StringUtil.append(paramForm.getPackagename()));
		List<PackageInfo> devicelist = packagedao.queryPackagelist(paramForm);
		return devicelist;
	}

	// 分页查询套餐列表
	@Override
	public PageModel<PackageInfo> queryPackagelistPage(PackageForm paramForm) {

		PageModel<PackageInfo> pageModel = new PageModel<PackageInfo>();
		if (StringUtil.isNotEmpty(paramForm.getPackagename())) {
			pageModel.getQueryparam().put("packagename", StringUtil.append(paramForm.getPackagename()));
		}

		if (StringUtil.isNotEmpty(paramForm.getPagecount())) {
			pageModel.setPagesize(Integer.parseInt(paramForm.getPagecount()));
		}
		Integer totleRows = packagedao.countRows(pageModel);
		pageModel.setTotlepage(totleRows);
		if (StringUtil.isNotEmpty(paramForm.getPagenumber())) {
			pageModel.setCurrentpage(Integer.parseInt(paramForm.getPagenumber()));
		}
		List<PackageInfo> packageList = packagedao.queryPackagelistPage(pageModel);
		pageModel.setQueryresult(packageList);

		return pageModel;
	}

	// 新增套餐
	@Override
	@Transactional
	public int addPackage(PackageInfo packageinfo, long operatorid) {
		if(packageinfo.getFee2() == null)
			packageinfo.setFee2(new BigDecimal("0"));
		if(packageinfo.getFreetime() == null)
			packageinfo.setFreetime(0);
		if(packageinfo.getTrytime() == null)
			packageinfo.setTrytime(0);
		int result = packagedao.addPackage(packageinfo);
		return result;
	}

	// 修改套餐
	@Override
	@Transactional
	public int modifyPackage(PackageInfo packageinfo, long operatorid) {
		if (packageinfo == null || packageinfo.getId() == null || StringUtil.isBlank(packageinfo.getPackagename()))
			return 0;
		int result = packagedao.updatePackage(packageinfo);
		return result;
	}

	// 删除
	@Override
	@Transactional
	public int delPackage(int id) {
		int result = packagedao.deletePackage(id);
		return result;
	}

}
